#include "client.hpp"
void Useage()
{
    std::cout << "Usage:./[client server_ip] [server_port]" << std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Useage();
        exit(1);
    }
    std::string server_ip = argv[1];
    uint16_t server_port = atoi(argv[2]);
    TcpClient client(server_ip, server_port);
    client.Init();
    bool reconnect = true;
    int count = MAXRECONNECT;
    int err = 0;
    do
    {
        if (client.Connect() == false)
        {
            client.Init();
            std::cerr << "Connect server fail, retry..." << std::endl;
            count--;
            reconnect = true;
            continue;
        }
        reconnect = false;
        count = MAXRECONNECT;

    } while (reconnect && count > 0);

    // std::string send_msg;
    // std::cout << "Please Enter# ";
    // std::cin >> send_msg;
    // client.Send(send_msg);
    // std::string message;
    // int RecvNum = client.Recv(message);
    // if (RecvNum < 0)
    // {
    //     std::cerr << "Recv fail" << std::endl;
    // }
    // std::cout << "server say :" << message << std::endl;

    int a = 0;
    int b = 0;
    std::string op;
    std::cin >> a >> op >> b;
    Request req(a, b, op);
    std::string request = req.Serialize();
    Encode(request);
    bool err = client.Send(req);
    if (err == false)
    {
        std::cerr << "Send fail" << std::endl;
    }
    Response res;
    err = client.Recv(res);
    if (err == false)
    {
        std::cerr << "Recv fail" << std::endl;
    }
    
    close(client.GetFd());
    return 0;
}